package org.aurora.core.biz.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.aurora.core.biz.persistence.ClientAppMapper;
import org.aurora.core.biz.service.IClientAppService;
import org.aurora.core.common.util.HashSlotUtil;
import org.aurora.model.entity.ClientAppInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class ClientAppServiceImpl implements IClientAppService {
    @Resource
    private ClientAppMapper clientAppMapper;

    @Override
    public List<ClientAppInfo> getClientAppInfosByHashSlots(List<Integer> hashSlots) {
        if (CollectionUtils.isEmpty(hashSlots)) {
            return Collections.emptyList();
        }

        return this.clientAppMapper.getClientAppInfosByHashSlots(hashSlots);
    }

    @Override
    public Long saveClientApp(ClientAppInfo clientAppInfo) {
        clientAppInfo.setHashSlot(HashSlotUtil.reHashCode(clientAppInfo.getAppcode()));
        this.clientAppMapper.saveClientApp(clientAppInfo);
        return clientAppInfo.getId();
    }
}
